import html
from utils.file_operation import load_json_to_dict
from rj.request import RequestHanlder
import time

rj = RequestHanlder()
web_data = load_json_to_dict("abcam_online_all.json")
upload_data = load_json_to_dict("abcam_price_new.json")

# 解码 HTML 实体
# decoded_html = html.unescape("&micro;")

# 输出解码后的字符串
# print(decoded_html)

for product in web_data:
    for item in upload_data:
        if product["code"] == item["code"]:

            # 新skuinfo
            new_skuInfos = []
            sku_dict = {sku["id"]: sku for sku in product['skuInfos']}

            for spec in item["spec"]:
                new_code = f"{item['code']}-{spec['spec']}"
                new_price = float(spec["price"])

                if sku_dict:
                    # 更新现有 SKU 信息
                    existing_sku = sku_dict.get(list(
                        sku_dict.keys())[0])  # 获取第一个 id 对应的 SKU
                    new_skuInfos.append({
                        "id":
                        existing_sku["id"],  # 使用已有的 id
                        "code":
                        new_code,  # 更新 code
                        "price":
                        int(new_price * 0.7),  # 更新 price
                        "directoryPrice":
                        new_price*1.1,
                        "differentPrices":[
                            {
                                "orgId": 3,
                                "price": int(new_price * 0.68)
                            }
                        ],
                        "stock":
                        1997,
                        "unit":
                        "件",
                        "carryFeeTemplateId":
                        2854,
                        "specification": {
                            "packingSpecificationValue": 0.0,
                            "packingSpecificationUnit": "",
                            "minSpecificationValue": 0.0,
                            "minSpecificationUnit": ""
                        },
                        "skuAttributes": [{
                            "attributeName": "产品规格",
                            "attributeValue": spec['spec']
                        }]
                    })
                    # 删除已处理的 SKU
                    del sku_dict[existing_sku["id"]]
                else:
                    # 新增 SKU 信息
                    new_skuInfos.append({
                        "code":
                        new_code,  # 新的 code
                        "price":
                        int(new_price * 0.7),  # 更新 price
                        "directoryPrice":
                        new_price*1.1,
                        "differentPrices":[
                            {
                                "orgId": 3,
                                "price": int(new_price * 0.68)
                            }
                        ],
                        "stock":
                        1997,
                        "unit":
                        "件",
                        "carryFeeTemplateId":
                        2854,
                        "specification": {
                            "packingSpecificationValue": 0.0,
                            "packingSpecificationUnit": "",
                            "minSpecificationValue": 0.0,
                            "minSpecificationUnit": ""
                        },
                        "skuAttributes": [{
                            "attributeName": "产品规格",
                            "attributeValue": spec['spec']
                        }]
                    })
            # print(sku_dict)
            product['skuInfos'] = new_skuInfos
            res = rj.batchSaveOrUpdateProductV2([product])
            print(f'正在上传{item['code']}')
            print(res.json())
            time.sleep(1)
